package com.yige.web.controller

import com.mongodb.BasicDBObject
import com.yige.common.DateUtil
import com.yige.common.ResultUtil
import com.yige.common.SystemConstant
import com.yige.service.mongoService.MongoAccountsService
import com.yige.service.mongoService.MongoIncomeService
import com.yige.service.mongoService.MongoStudentService
import com.yige.web.timer.AccountsTimer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by sunhao on 2015/11/16 0016.
 */

@Controller
@RequestMapping("accounts")
class AccountsController {

    @Autowired
    MongoAccountsService accountsService

    @Autowired
    MongoIncomeService incomeService

    @Autowired
    MongoStudentService studentService

    @Autowired
    AccountsTimer accountsTimer


    /*****************控制器模块*****************/

    /**
     * 欠费列表
     * @param model
     * @return
     */
    @RequestMapping("list.do")
    def list(Model model){
        accountsTimer.execute()
        def accountList = accountsService.findListDescendingByDate(),accounts = new ArrayList()
        def map = new HashMap(),msg = "未结清"
        accountList.each {
            map.put("_id",it._id)
            map.put("student",it.student)
            map.put("action",it.action)
            if(it.status == 1){
                msg = "已结清"
            }
            map.put("status",msg)
            map.put("date",DateUtil.formatDate(it.date))
            accounts.add(map)
            map = new HashMap()
        }
        stuArg(model)
        model.addAttribute("accounts",accounts)
        return "accounts/list"
    }

    @RequestMapping("find")
    def find(Model model,String startDate, String endDate,int status, String student){
        def query = new BasicDBObject(),accounts = new ArrayList()
        def map = new HashMap(),msg = "未结清"
        if (!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)) {
            query.append("date", [$gte: DateUtil.parseDate(startDate), $lte: DateUtil.parseDate(endDate)] as BasicDBObject)
        }
        if(!StringUtils.isEmpty(student)){
            query.append("student",student)
        }
        if(status != -1){
            query.append("status",status)
        }
        def accountList = accountsService.findListByQueryDesDate(query)
        accountList.each {
            map.put("_id",it._id)
            map.put("student",it.student)
            map.put("action",it.action)
            if(it.status == 1){
                msg = "已结清"
            }
            map.put("status",msg)
            map.put("date",DateUtil.formatDate(it.date))
            accounts.add(map)
            map = new HashMap()
        }
        stuArg(model)
        model.addAttribute("accounts",accounts)
        return "accounts/list"
    }

    /**
     * 结清欠款
     * @param _ids
     * @return
     */
    @RequestMapping("pay")
    @ResponseBody
    def pay(@RequestParam(value = "_ids") List<String> _ids){
        boolean flag = false
        def _id
        _ids.each {
            _id = accountsService.findOneById(it).income
            if(_id != null){
                flag = incomeService.changeStatus(_id,SystemConstant.INCOME_PAY)
                accountsTimer.execute()
            }
        }
        return ResultUtil.recallAjax(flag)
    }



    /*************方法模块***************/

    def stuArg(Model model){
        def studentList = studentService.findList()
        model.addAttribute("students",studentList)
    }


}
